package com.lxk.controller;

import com.lxk.pojo.Admin;
import com.lxk.service.AdminService;
import com.lxk.vo.PageModel;
import com.lxk.vo.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author JohnSoybean
 * @create 2020/4/13 11:25
 * @description
 */
@Controller
public class AdminController {

    @Autowired
    private AdminService as;

    /**
     * 展示拍卖列表
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/admin")
    public String toAdminManage(HttpServletRequest req, Model model,
                                Integer pageIndex, Integer num, Integer totalPages,
                                Integer sort, Integer order ) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 12;
        }
        if (null == totalPages) {
            totalPages = as.getTotalPages(num);
        }
        PageModel<Admin> adminInfo = as.selAdminPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", adminInfo);
        return "admin";
    }

    /**
     * 按ID查找管理员
     * @param adminId
     * @return
     */
    @GetMapping("/admin/query")
    @ResponseBody
    public Result<Admin> queryAdmin(Integer adminId) {
        Admin admin = as.selById(adminId);
        return Result.success(admin);
    }


    /**
     * 删除管理员
     * @param adminId
     * @return
     */
    @GetMapping("/admin/delete")
    @ResponseBody
    public Result<Integer> deleteAdmin(Integer adminId) {
        as.delAdmin(adminId);
        return Result.success(1);
    }

    /**
     * 添加管理员
     * @param admin
     * @return
     */
    @PostMapping("/admin/add")
    @ResponseBody
    public Result<Integer> addAdmin(Admin admin) {
        as.addAdmin(admin);
        return Result.success(1);
    }

    /**
     * 修改管理员
     * @param admin
     * @return
     */
    @PostMapping("/admin/modify")
    @ResponseBody
    public Result<Integer> modifyAdmin(Admin admin) {
        as.modifyAdmin(admin);
        return Result.success(1);
    }

}
